<?php

namespace App\Http\Controllers\CallingCards\Merchant;

use App\Enums\CallingCards\BaseEnum;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\CallingCards\MerchantAppAuth;
use App\Models\Common\ConfigModel;
use App\Models\FeishuCallingCard\FeishuCallingCardFranchisee;
use App\Models\FeishuCallingCard\FeishuCallingCardFranchiseeClue;
use App\Models\FeishuCallingCard\FeishuCallingCardFranchiseeSignRecord;
use Illuminate\Support\Facades\Cache;

class CallingCardsController extends BaseController
{

    public function getCallingCardsInfoByUser()
    {
        $clueCount     = FeishuCallingCardFranchiseeClue::query()
            ->where('invitorId', 'u' . MerchantAppAuth::$userInfo['id'])
            ->count();
        $clueCount     += FeishuCallingCardFranchiseeClue::query()
            ->where('invitorId', 'like', 'u' . MerchantAppAuth::$userInfo['id'] . 's%')
            ->count();
        $signRecords   = FeishuCallingCardFranchiseeSignRecord::query()
            ->where('uid', MerchantAppAuth::$userInfo['id'])
            ->count();
        $grandTotal    = FeishuCallingCardFranchisee::query()
            ->where('uid', MerchantAppAuth::$userInfo['id'])
            ->value('prizeNum');
        $dayCount      = ceil((time() - MerchantAppAuth::$userInfo['createdAt']) / 86400);
        $announcements = [];
        $rule          = '';
        $homeImg       = 'https://file.51kuafu.com/9f/74/01352d770a430fcdc0a5d3e6a5386f4fc557ac3fae0e1d5697309d23a57a.jpg';
        $detailImg     = 'https://file.51kuafu.com/51/26/777950e584a4dc6e304e9e763b7710a9b558e6d046103f76d0d3cfde2dcb.jpg';
        ConfigModel::query()
            ->where('module', 'callingCards')
            ->get()
            ->each(function ($item) use (&$announcements, &$rule, &$homeImg, &$detailImg) {
                if ($item->name == 'announcement') {
                    $announcements = json_decode($item->value, true);
                }
                if ($item->name == 'rule') {
                    $rule = $item->value;
                }
                if ($item->name == 'homeImg') {
                    $homeImg = $item->value;
                }
                if ($item->name == 'detailImg') {
                    $detailImg = $item->value;
                }
            });
        $data = [
            'name'           => MerchantAppAuth::$userInfo['username'],
            'position'       => MerchantAppAuth::$franchisee ? MerchantAppAuth::$franchisee->position : '',
            'image'          => MerchantAppAuth::$userInfo['avatar'],
            'time'           => '', // 成为夸父合伙人的第x天
            'mobile'         => MerchantAppAuth::$userInfo['mobile'],
            'address'        => MerchantAppAuth::$userInfo['address'],
            'contractImg'    => $homeImg,
            'contractDesc'   => $rule,
            'invitationImg'  => $detailImg,
            'invitationInfo' => [
                [
                    'name'  => '线索收集量',
                    'count' => $clueCount ?: 0,
                ],
                [
                    'name'  => '签约成功量',
                    'count' => $signRecords ?: 0,
                ],
                [
                    'name'  => '累计奖励',
                    'count' => $grandTotal ?: 0,
                ],
            ],
            'notices'        => $announcements
        ];
        return $this->success($data);
    }

}
